﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AmuTools;
using app.Api; 

namespace app.Models
{
    [Model(TableName = "t_admin", PrimaryKey = "id", IdentityInsert = false)]
    public class Admin : ModelBase
    {
        [Field(Description = "唯一标识符")]
        public int id { get; set; }


        [Field(Description = "用户名")]
        public string username { get; set; }


        [Field(Webable = false, DataType = "varchar(50)", Description = "密码")]
        public string password { get; set; }


        [Field(Webable = false, Description = "角色ID")]
        public int role_id { get; set; }


        private AdminRole _role = null;
        [Field(Storageable = false, Description = "角色")]
        public AdminRole role
        {
            get
            {
                if (_role == null)
                {
                    _role = G.DB.GetById<AdminRole>(this.role_id);
                }
                return _role;
            }
            set
            {
                _role = value;
                role_id = _role.id;
            }
        }


        [Field(Webable = false, DataType = "text", Description = "特殊权限，即除角色权限以外的权限")]
        public string special_permission { get; set; }


        private string _permission = null;
        [Field(Storageable = false, Description = "权限，包含角色权限和特殊权限")]
        public string permission {
            get
            {
                if (_permission == null)
                {
                    _permission = (role == null ? "" : role.permission) + this.special_permission;
                }
                return _permission;
            }
        }


        [Field(Description = "状态，目前好像没有用到")]
        public int status { get; set; }


        [Field(Webable = false, Description = "是否已经被删除")]
        public int is_delete { get; set; }


        [Field(Storageable = false, Description = "登陆状态：0离线，1在线，2离开")]
        public int landing_state {
            get
            {
                return (int)Api.LandingServer.GetUnitState(this.id.ToString());
            }
        }

        
        [Field(Storageable = false, Description = "上次登陆的日志")]
        public Log last_login_log
        {
            get
            {
                Cd cd = new Cd();
                cd.And("user_id='{0}'", api_base.UserCode);
                cd.And("operation='{0}'", (int)PERMISSIONOPERATION.LOGIN);
                Ob ob = new Ob();
                ob.Desc("create_time");
                List<Log> list = G.DB.Get<Log>(cd.Render(), ob.Render(), 2).GetFirstTableList();
                return list.Count == 2 ? list[1] : null;
            }
        }


        [Field(Webable = false, Default = 0, Description = "是否必须，如果为1则意味着这个账号是不可被删除的，只有超级管理员用户才不会被删除")]
        public int? is_must { get; set; }

        
        // 初始化数据，内置了超级管理员用户
        public static Admin[] GetInitData()
        {
            return new Admin[]{
                new Admin { username = "admin", password = HashHelper.Hash_MD5_16("123456"), special_permission = "+100010", is_must = 1 }
            };
        }
    }
    
}